<?php

namespace App\Admin\Extensions;

use App\Models\CompanyModel;
use App\Models\User;
use App\Models\UserEducationModel;
use App\Models\UserWorkExperienceModel;
use Box\Spout\Common\Entity\Style\CellAlignment;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Dcat\EasyExcel\Excel;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Common\Entity\Style\Color;
class ExcelExpoter extends AbstractExporter
{
    public function export()
    {
        // 这段逻辑是从表格数据中取出需要导出的字段
        $rows = collect($this->buildData(1, 100000))->map(function ($row) {
                    $user_education = UserEducationModel::where('user_id', $row['user_id'])->first();
                    $user_work_experience = UserWorkExperienceModel::where('user_id', $row['user_id'])->get();
                    $work_experience = '';//工作经历
                    //教育经历
                    if ($user_education) {
                        $education =
                            "学校：{$user_education->school_name}学历：{$user_education->degree}毕业时间：{$user_education->graduation_date}专业：{$user_education->major}";
                    } else {
                        $education = '暂无';
                    }
                    foreach ($user_work_experience as $item){
                        $work_experience .= "公司名称：{$item->company_name}职位名称：{$item->job_title}时间：{$item->start_date}-{$item->end_date}职位描述：{$item->job_description}\n";
                    }
                    $company =  $row['company.cur_type'] == CompanyModel::TYPE_PERSON ? $row['company.contact_person_name'] :$row['company.company_name'];
            $arr = [];
                    $arr['company_name'] = $company;
                    $arr['position_name'] = $row['position_name'];
                    $arr['user_name'] = $row['user_info.name']??'';
                    $arr['user_age'] =  12;
                    $arr['user_mobile'] = $row['user_info.mobile']??'';
                    $arr['user_gender'] = User::GENDER_LIST[$row['user_info.gender']]??'';
                    $arr['user_identity'] = User::IDENTITY_LIST[$row['user_info.identity']]??'';
                    $arr['user_self_description'] = $row['user_info.self_description']??'';
                    $arr['user_education'] = $education;
                    $arr['user_work_experience'] = $work_experience;
                    return $arr;
        });
        $titles = [
            'company_name' => '公司',
            'position_name' => '职位名称',
            'user_name' => '用户姓名',
            'user_gender' => '用户性别',
            'user_age' => '用户年龄',
            'user_mobile' => '联系方式',
            'user_identity' => '身份',
            'user_self_description' => '自我描述',
            'user_education' => '教育经历',
            'user_work_experience' => '工作经历',
            'created_at' => '报名时间',
        ];

//        dd($titles);
        Excel::export($rows)
            ->headings($titles)
            ->row(function (array $row) {
                // 设置样式
                $style = (new StyleBuilder())
                    ->setFontSize(12)
                    ->setCellAlignment(CellAlignment::CENTER)
                    ->setShouldWrapText()
                    ->build();
// 设置列宽自适应
                return WriterEntityFactory::createRowFromArray($row, $style);
            })
            ->download('报名列表.xlsx');
    }
}